package jiyun.com.yiyanmeng;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.superluo.textbannerlibrary.TextBannerView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import adapter.KeChengAdapter;
import base.BaseFragment;
import bean.KechengBannerBean;
import presenter.KeChengPresenter;
import utils.LogUtil;
import view.KeChengView;


/**
 * A simple {@link Fragment} subclass.
 */
public class KeChengFragment extends BaseFragment<KeChengView, KeChengPresenter> implements KeChengView{

    private Banner kecheng_banner;
    private TextBannerView kecheng_textbanner;
    private RecyclerView kecheng_rv;

    public static KeChengFragment newInstance() {

        Bundle args = new Bundle();

        KeChengFragment fragment = new KeChengFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void showToast(String msg) {

    }

    @Override
    protected KeChengPresenter bindPresenter() {
        return new KeChengPresenter(this);
    }

    @Override
    protected void initData() {
        mPresenter.getData();

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView(View view) {
        kecheng_textbanner = view.findViewById(R.id.kecheng_textbanner);
        kecheng_banner = view.findViewById(R.id.kecheng_banner);
        kecheng_rv = view.findViewById(R.id.kecheng_rv);

    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_kecheng;
    }

    @Override
    public void onKechengBannerSuccess(KechengBannerBean kechengBannerBean) {
        ArrayList<String> images = new ArrayList<>();
        for (int i = 0; i < kechengBannerBean.getLunbo().size(); i++) {
            String url = kechengBannerBean.getLunbo().get(i).getPic();
            //LogUtil.log("banner"+url);
            Log.i("liuth", "onKechengBannerSuccess: "+url);
            images.add(url);
        }
        kecheng_banner.setImages(images);
        kecheng_banner.setImageLoader(new ImageLoad());
        kecheng_banner.start();
        //textBanner
        ArrayList<String> texts = new ArrayList<>();
        for (int i = 0; i < kechengBannerBean.getGong_gao().size(); i++) {
            String url = kechengBannerBean.getGong_gao().get(i).getName();
            LogUtil.log("textBanner"+url);
            texts.add(url);
        }
        kecheng_textbanner.setDatas(texts);
        //rv
        //设置适配器
        kecheng_rv.setLayoutManager(new LinearLayoutManager(getContext()));
        //分割线
        kecheng_rv.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        //数据源
        List<KechengBannerBean.KechengBean> kecheng = kechengBannerBean.getKecheng();
        ArrayList<KechengBannerBean.KechengBean> list = new ArrayList<>();
        list.addAll(kecheng);
        //创建适配器
        KeChengAdapter adapter = new KeChengAdapter(getActivity(), list);
        //绑定适配器
        kecheng_rv.setAdapter(adapter);
        //条目点击
        adapter.setOnItemClickListener(new KeChengAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(context, "点击", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), KeChengWebActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onKechengBannerFail(String error) {

    }

    public class ImageLoad extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(getContext()).load(path).into(imageView);
        }
    }
}
